package dao

import (
	"github.com/jinzhu/gorm"
	"study_abroad_system_back_end/common"
)

var (
	DB *gorm.DB
)

func InitMySQL(config *common.Config) (err error) {
	database := config.Database
	dsn := database.User + ":" +database.Password +"@tcp("+
		database.Host + ":" + database.Port + ")/" + database.DbName+
		"?charset=" + database.Charset + "&parseTime=" + database.ParseTime +"&loc="+ database.Loc
	//dsn := "root:123456@tcp(127.0.0.1:3306)/study_abroad_system?charset=utf8mb4&parseTime=True&loc=Local"
	DB, err =gorm.Open(database.Driver, dsn)
	if err != nil {
		return
	}
	return DB.DB().Ping()
}

func GetDB() *gorm.DB{
	return DB
}